home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / intuition / 8hamdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  8.8 KB  |  396 lines

  1. /*
  2.  * 8hamdemo.c - shows off 262144 colors simultaneously
  3.  *
  4.  * (c) Copyright 1992-1999 Amiga, Inc.  All rights reserved.
  5.  *
  6.  * This software is provided as-is and is subject to change; no warranties
  7.  * are made.  All use is at your own risk.  No liability or responsibility
  8.  * is assumed.
  9.  *
  10.  */
  11.  
  12. #include <graphics/gfxbase.h>
  13. #include <graphics/displayinfo.h>
  14. #include <intuition/intuitionbase.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <clib/diskfont_protos.h>
  19. #include <clib/intuition_protos.h>
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. /*----------------------------------------------------------------------*/
  25.  
  26. void error_exit(STRPTR errorstring);
  27.  
  28. int CXBRK(void) { return(0); }
  29. void chkabort(void) { return; }
  30.  
  31. /*----------------------------------------------------------------------*/
  32.  
  33. struct Library *DiskfontBase = NULL;
  34. struct GfxBase *GfxBase = NULL;
  35. struct IntuitionBase *IntuitionBase = NULL;
  36. struct Screen *myscreen = NULL;
  37. struct Window *mywindow = NULL;
  38. struct BitMap *bm[2] = {NULL,NULL};
  39. struct RastPort temprp;
  40. struct TextFont *font = NULL;
  41.  
  42. /*----------------------------------------------------------------------*/
  43.  
  44. UWORD pens[] =
  45. {
  46.     0, /* DETAILPEN */
  47.     1, /* BLOCKPEN    */
  48.     1, /* TEXTPEN    */
  49.     2, /* SHINEPEN    */
  50.     1, /* SHADOWPEN    */
  51.     3, /* FILLPEN    */
  52.     1, /* FILLTEXTPEN    */
  53.     0, /* BACKGROUNDPEN    */
  54.     2, /* HIGHLIGHTTEXTPEN    */
  55.  
  56.     1, /* BARDETAILPEN    */
  57.     2, /* BARBLOCKPEN    */
  58.     1, /* BARTRIMPEN    */
  59.  
  60.     (UWORD)~0,
  61. };
  62.  
  63.  
  64. /* Color table to pass to SA_Colors32 or LoadRGB32() */
  65. ULONG colortable[] =
  66. {
  67.     0x00040000,    /* 5 colors to load, from zero */
  68.  
  69.     0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA,    /* Gray */
  70.     0x00000000, 0x00000000, 0x00000000,    /* Black */
  71.     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,    /* White */
  72.     0x66666666, 0x88888888, 0xBBBBBBBB,    /* Blue-gray */
  73.  
  74.     0x00000000,    /* terminator */
  75. };
  76.  
  77. /*----------------------------------------------------------------------*/
  78.  
  79. #define PLOTWIDTH 64
  80. #define PLOTHEIGHT 64
  81.  
  82.  
  83. /*----------------------------------------------------------------------*/
  84.  
  85. /* Let's arrange the blue values in a spiral, just to be cute.
  86.  * We'll do it with a table instead of an algorithm out of
  87.  * laziness.
  88.  */
  89. int bluespiral1[] =
  90. {
  91.     56, 55, 54, 53, 52, 51, 50, 49,
  92.     57, 30, 29, 28, 27, 26, 25, 48,
  93.     58, 31, 12, 11, 10,  9, 24, 47,
  94.     59, 32, 13,  2,  1,  8, 23, 46,
  95.     60, 33, 14,  3,  0,  7, 22, 45,
  96.     61, 34, 15,  4,  5,  6, 21, 44,
  97.     62, 35, 16, 17, 18, 19, 20, 43,
  98.     63, 36, 37, 38, 39, 40, 41, 42,
  99. };
  100.  
  101. int bluespiral2[] =
  102. {
  103.     60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45,
  104.     61, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 44,
  105.     62, 27,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 43,
  106.     63, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
  107. };
  108.  
  109. /*----------------------------------------------------------------------*/
  110.  
  111. struct TextAttr topaz80 =
  112. {
  113.     "topaz.font",
  114.     8,
  115.     0,
  116.     0,
  117. };
  118.  
  119. struct TextAttr preffont =
  120. {
  121.     "times.font",
  122.     15,
  123.     0,
  124.     0,
  125. };
  126.  
  127. #define MOD_BLUE    64
  128. #define MOD_RED        128
  129. #define MOD_GREEN    192
  130.  
  131. #define XOFF 6
  132. #define YOFF 5
  133.  
  134. main(argc, argv)
  135. int argc;
  136. char *argv[];
  137. {
  138.     int blue, x, ybase, column;
  139.     int redreverse, red, dred;
  140.     int left, top, width, height, b;
  141.     int perrow = 16;
  142.     int *bluespiral = bluespiral2;
  143.     struct TextAttr *scfont = &topaz80;
  144.  
  145.     ULONG displayID = NTSC_MONITOR_ID | SUPERHAMLACE_KEY;
  146.     int overscan = OSCAN_TEXT;
  147.  
  148.     if ( ( argc > 1 ) && ( *argv[1] == '?' ) )
  149.     {
  150.     printf("8HAMDemo [S|P|D|N]\n");
  151.     printf("- S = use Super72\n");
  152.     printf("- P = use PAL hires-lace\n");
  153.     printf("- D = use double-PAL hires-lace\n");
  154.     printf("- N = use NTSC superhires-lace [default]\n");
  155.     }
  156.     else
  157.     {
  158.     if ( ( argc > 1 ) && ( ( *argv[1] == 'D' ) || ( *argv[1] == 'd' ) ) )
  159.     {
  160.         displayID = DBLPALHIRESHAMFF_KEY;
  161.         overscan = OSCAN_MAX;
  162.         perrow = 8;
  163.             bluespiral = bluespiral1;
  164.     }
  165.  
  166.     if ( ( argc > 1 ) && ( ( *argv[1] == 'P' ) || ( *argv[1] == 'p' ) ) )
  167.     {
  168.         displayID = PAL_MONITOR_ID | HIRESHAMLACE_KEY;
  169.         overscan = OSCAN_MAX;
  170.         perrow = 8;
  171.             bluespiral = bluespiral1;
  172.     }
  173.  
  174.     if ( ( argc > 1 ) && ( ( *argv[1] == 'S' ) || ( *argv[1] == 's' ) ) )
  175.     {
  176.         displayID = 0x89824;
  177.         overscan = OSCAN_TEXT;
  178.         perrow = 8;
  179.             bluespiral = bluespiral1;
  180.     }
  181.  
  182.     if ( ( argc > 1 ) && ( ( *argv[1] == 'N' ) || ( *argv[1] == 'n' ) ) )
  183.     {
  184.         displayID = NTSC_MONITOR_ID | SUPERHAMLACE_KEY;
  185.         overscan = OSCAN_TEXT;
  186.         perrow = 16;
  187.             bluespiral = bluespiral2;
  188.     }
  189.  
  190.     if (!( GfxBase = (struct GfxBase *)
  191.         OpenLibrary("graphics.library", 39L) ))
  192.         {
  193.         error_exit("Couldn't open Gfx V39\n");
  194.         }
  195.  
  196.     if (!( IntuitionBase = (struct IntuitionBase *)
  197.         OpenLibrary("intuition.library", 39L) ))
  198.         {
  199.         error_exit("Couldn't open Intuition V39\n");
  200.         }
  201.  
  202.     if (!( DiskfontBase = (struct Library *)
  203.         OpenLibrary("diskfont.library", 37L) ))
  204.         {
  205.         error_exit("Couldn't open Diskfont V37\n");
  206.         }
  207.  
  208.     if ( font = OpenDiskFont( &preffont ) )
  209.     {
  210.         scfont = &preffont;
  211.     }
  212.  
  213.     InitRastPort( &temprp );
  214.     bm[0] = AllocBitMap( PLOTWIDTH, PLOTHEIGHT, 8, BMF_INTERLEAVED|BMF_CLEAR, NULL );
  215.     temprp.BitMap = bm[0];
  216.     for ( x=0; x<64; x++ )
  217.     {
  218.         SetAPen( &temprp, MOD_GREEN+x );
  219.         Move( &temprp, x, 0 );
  220.         Draw( &temprp, x, 63 );
  221.     }
  222.  
  223.     bm[1] = AllocBitMap( PLOTWIDTH, PLOTHEIGHT, 8, BMF_INTERLEAVED|BMF_CLEAR, NULL );
  224.     temprp.BitMap = bm[1];
  225.  
  226.     for ( x=0; x<64; x++ )
  227.     {
  228.         SetAPen( &temprp, MOD_GREEN+63-x );
  229.         Move( &temprp, x, 0 );
  230.         Draw( &temprp, x, 63 );
  231.     }
  232.  
  233.     if (!(myscreen = OpenScreenTags(NULL,
  234.         SA_DisplayID, displayID,
  235.         SA_Overscan, overscan,
  236.         /*  Other tags can go here: */
  237.         SA_Font, scfont,
  238.         SA_Depth, 8,
  239.         SA_AutoScroll, 1,
  240.         SA_Pens, pens,
  241.         SA_Interleaved, TRUE,
  242.         SA_Title, "8-plane HAM Demo",
  243.         SA_Colors32, colortable,
  244.         SA_Behind, TRUE,
  245.         TAG_DONE )))
  246.         {
  247.         error_exit("Couldn't open screen\n");
  248.         }
  249.  
  250.     width = 14 + (perrow*65);
  251.     height = 10 + scfont->ta_YSize + (64/perrow)*64;
  252.     left = ( myscreen->Width - width - scfont->ta_YSize - 2 ) / 2;
  253.     top = ( myscreen->Height - height ) / 2;
  254.  
  255.     if (!( mywindow = OpenWindowTags(NULL,
  256.         WA_Left, left,
  257.         WA_Top, top,
  258.         WA_Width, width,
  259.         WA_Height, height,
  260.         WA_MinWidth, 50,
  261.         WA_MinHeight, 40,
  262.         WA_MaxWidth, -1,
  263.         WA_MaxHeight, -1,
  264.         WA_IDCMP, IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY,
  265.         WA_DepthGadget, TRUE,
  266.         WA_DragBar, TRUE,
  267.         WA_Title, "262144 Colors!",
  268.         WA_CloseGadget, TRUE,
  269.         WA_NoCareRefresh, TRUE,
  270.         WA_SmartRefresh, TRUE,
  271.         WA_CustomScreen, myscreen,
  272.         WA_Activate, TRUE,
  273.         TAG_DONE ) ))
  274.         {
  275.         error_exit("Couldn't open window\n");
  276.         }
  277.  
  278.     b = 0;
  279.     ybase = 0;
  280.     column = 0;
  281.     redreverse=0;
  282.  
  283.     for ( blue = 0; blue < 64; blue++ )
  284.     {
  285.         if ( column == 0 )
  286.         {
  287.         int dy;
  288.  
  289.         SetAPen( mywindow->RPort, 1 );
  290.         Move( mywindow->RPort, XOFF-1, (YOFF+scfont->ta_YSize)+ybase );
  291.         Draw( mywindow->RPort, XOFF-1, (YOFF+scfont->ta_YSize)+ybase+63 );
  292.  
  293.         for ( dy = 0; dy < 64; dy++ )
  294.         {
  295.             if ( redreverse )
  296.             {
  297.             SetAPen( mywindow->RPort, MOD_RED + 63-dy );
  298.             }
  299.             else
  300.             {
  301.             SetAPen( mywindow->RPort, MOD_RED + dy );
  302.             }
  303.             WritePixel( mywindow->RPort, XOFF, (YOFF+scfont->ta_YSize)+ybase+dy );
  304.         }
  305.         }
  306.  
  307.         SetAPen( mywindow->RPort, MOD_BLUE+63-bluespiral[blue]);
  308.         Move( mywindow->RPort, XOFF+column*65+1, (YOFF+scfont->ta_YSize)+ybase );
  309.         Draw( mywindow->RPort, XOFF+column*65+1, (YOFF+scfont->ta_YSize)+ybase+63 );
  310.  
  311.         BltBitMapRastPort( bm[b], 0, 0,
  312.         mywindow->RPort, XOFF+column*65 + 2, (YOFF+scfont->ta_YSize)+ybase,
  313.         PLOTWIDTH, PLOTHEIGHT, 0xC0 );
  314.         b = 1-b;
  315.  
  316.         column++;
  317.         if ( column == perrow )
  318.         {
  319.         column = 0;
  320.         ybase += 64;
  321.         redreverse = 1-redreverse;
  322.         }
  323.     }
  324.     ScreenToFront( myscreen );
  325.     dred = 1; red = 0;
  326.     /* Exit on break or on any IntuiMessage */
  327.     while ( !( SetSignal(0,0) & ( ( 1 << mywindow->UserPort->mp_SigBit ) | SIGBREAKF_CTRL_C ) ) )
  328.     {
  329.         ScrollRaster( mywindow->RPort, 0, -1, XOFF, (YOFF+scfont->ta_YSize), XOFF, (YOFF+scfont->ta_YSize)+(4096/perrow)-1 );
  330.         SetAPen( mywindow->RPort, MOD_RED + red );
  331.         red += dred;
  332.         if ( red == -1 )
  333.         {
  334.         dred = 1;
  335.         red = 0;
  336.         }
  337.         else if (red == 64 )
  338.         {
  339.         red = 63;
  340.         dred = -1;
  341.         }
  342.         WritePixel( mywindow->RPort, XOFF, (YOFF+scfont->ta_YSize) );
  343.     }
  344.     }
  345.     error_exit(NULL);
  346. }
  347.  
  348.  
  349. /*----------------------------------------------------------------------*/
  350.  
  351. void error_exit(STRPTR errorstring)
  352.  
  353.     {
  354.     if (mywindow)
  355.     {
  356.     ScreenToBack(myscreen);
  357.     CloseWindow(mywindow);
  358.     }
  359.  
  360.     if (myscreen)
  361.     {
  362.     CloseScreen(myscreen);
  363.     }
  364.  
  365.     if ( bm[1] ) FreeBitMap( bm[1] );
  366.     if ( bm[0] ) FreeBitMap( bm[0] );
  367.  
  368.     if (font) CloseFont(font);
  369.  
  370.     if (DiskfontBase)
  371.     {
  372.     CloseLibrary(DiskfontBase);
  373.     }
  374.  
  375.     if (IntuitionBase)
  376.     {
  377.     CloseLibrary(IntuitionBase);
  378.     }
  379.  
  380.     if (GfxBase)
  381.     {
  382.     CloseLibrary(GfxBase);
  383.     }
  384.  
  385.     if (errorstring)
  386.     {
  387.     printf(errorstring);
  388.     exit(20);
  389.     }
  390.  
  391.     exit(0);
  392.     }
  393.  
  394.  
  395. /*----------------------------------------------------------------------*/
  396.